package com.huawei.appgallery.apkmanagement.impl.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import com.huawei.hms.framework.network.restclient.hwhttp.trans.FileBinary;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import o.bbu;

public class ExternalApkFileProvider extends ContentProvider {
    private static final String[] ˋ = new String[]{"_display_name", "_size"};
    private String ॱ;

    public boolean onCreate() {
        return true;
    }

    public void attachInfo(Context context, ProviderInfo providerInfo) {
        super.attachInfo(context, providerInfo);
        if (providerInfo.exported) {
            throw new SecurityException("Provider must not be exported");
        } else if (providerInfo.grantUriPermissions) {
            this.ॱ = providerInfo.authority;
        } else {
            throw new SecurityException("Provider must grant uri permissions");
        }
    }

    public Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) {
        File ॱ = bbu.ˊ(getContext()).ॱ(uri);
        if (strArr == null) {
            strArr = ˋ;
        }
        String[] strArr3 = new String[strArr.length];
        Object[] objArr = new Object[strArr.length];
        int length = strArr.length;
        int i = 0;
        int i2 = 0;
        while (i < length) {
            int i3;
            Object obj = strArr[i];
            if ("_display_name".equals(obj)) {
                strArr3[i2] = "_display_name";
                i3 = i2 + 1;
                objArr[i2] = ॱ.getName();
            } else if ("_size".equals(obj)) {
                strArr3[i2] = "_size";
                i3 = i2 + 1;
                objArr[i2] = Long.valueOf(ॱ.length());
            } else {
                i3 = i2;
            }
            i++;
            i2 = i3;
        }
        String[] ˎ = ˎ(strArr3, i2);
        Object[] ˎ2 = ˎ(objArr, i2);
        Cursor matrixCursor = new MatrixCursor(ˎ, 1);
        matrixCursor.addRow(ˎ2);
        return matrixCursor;
    }

    private static String[] ˎ(String[] strArr, int i) {
        Object obj = new String[i];
        System.arraycopy(strArr, 0, obj, 0, i);
        return obj;
    }

    private static Object[] ˎ(Object[] objArr, int i) {
        Object obj = new Object[i];
        System.arraycopy(objArr, 0, obj, 0, i);
        return obj;
    }

    public String getType(Uri uri) {
        if (!(uri == null || TextUtils.isEmpty(uri.toString()))) {
            List<String> ˊ = bbu.ˊ(getContext()).ˊ();
            String uri2 = uri.toString();
            for (String endsWith : ˊ) {
                String endsWith2;
                if (uri2.endsWith(endsWith2)) {
                    endsWith2 = MimeTypeMap.getSingleton().getMimeTypeFromExtension("apk");
                    if (endsWith2 != null) {
                        return endsWith2;
                    }
                }
            }
        }
        return FileBinary.HEAD_VALUE_CONTENT_TYPE_OCTET_STREAM;
    }

    public Uri insert(Uri uri, ContentValues contentValues) {
        throw new UnsupportedOperationException("No external inserts");
    }

    public int update(Uri uri, ContentValues contentValues, String str, String[] strArr) {
        throw new UnsupportedOperationException("No external updates");
    }

    public int delete(Uri uri, String str, String[] strArr) {
        throw new UnsupportedOperationException("No external updates");
    }

    public ParcelFileDescriptor openFile(Uri uri, String str) throws FileNotFoundException {
        if (uri != null && uri.getAuthority() != null && uri.getAuthority().equals(this.ॱ) && ॱ(uri.toString(), bbu.ˊ(getContext()).ˊ())) {
            return ParcelFileDescriptor.open(bbu.ˊ(getContext()).ॱ(uri), 268435456);
        }
        return null;
    }

    private boolean ॱ(String str, List<String> list) {
        for (String endsWith : list) {
            if (str.endsWith(endsWith)) {
                return true;
            }
        }
        return false;
    }
}
